package com.zeildlin.seecode.Controller;

import com.zeildlin.seecode.Service.LikeSerivce;
import com.zeildlin.seecode.entity.User;
import com.zeildlin.seecode.util.CommunityUtil;
import com.zeildlin.seecode.util.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
public class LikeController {
    @Autowired
    LikeSerivce likeSerivce;

    @Autowired
    HostHolder hostHolder;

    @PostMapping("/like")
    @ResponseBody
    public String like(int entityType,int entityId){
        User user = hostHolder.getUser();

        //点赞
        likeSerivce.like(user.getId(),entityType,entityId);
        //数量
        long likeCount = likeSerivce.findEntityLikeCount(entityType,entityId);
        //状态
        int likeStatus = likeSerivce.findEntityLikeStatus(user.getId(),entityType,entityId);
        Map<String,Object> map = new HashMap<>();
        map.put("likeCount",likeCount);
        map.put("likeStatus",likeStatus);
        return CommunityUtil.getJSONString(0,null,map);
    }
}
